/*
 * Copyright 2020-2021 redragon.dongbin
 *
 * This file is part of redragon-erp/赤龙ERP.

 * redragon-erp/赤龙ERP is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.

 * redragon-erp/赤龙ERP is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with redragon-erp/赤龙ERP.  If not, see <https://www.gnu.org/licenses/>.
 */
package com.erp.inv.input.dao.hibernate;

import com.erp.inv.input.dao.InvInputHeadDao;
import com.erp.inv.input.dao.model.InvInputHead;
import com.erp.inv.input.dao.model.InvInputHeadCO;
import com.erp.order.supply.model.SupplyMaterialBeanVo;
import com.erp.order.supply.model.SupplyMaterialDetailVO;
import com.erp.order.supply.model.SupplyMaterialRVO;
import com.erp.order.supply.model.SupplyMaterialVO;
import org.apache.fop.area.Page;
import org.redragon.framework.annotation.Permissions;
import org.redragon.framework.annotation.Permissions.PermissionType;
import org.redragon.framework.annotation.SqlParam;
import org.redragon.framework.hibernate.DaoSupport;
import org.redragon.framework.hibernate.model.Pages;
import org.redragon.framework.util.DaoCRUDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class InvInputHeadDaoImpl implements InvInputHeadDao{ 

    //注入DaoSupport工具类
    @Autowired
    private DaoSupport daoSupport;
    
    @Override
    public void insertDataObject(InvInputHead obj) {
        this.daoSupport.insertDataTransaction(obj);
    }

    @Override
    public void updateDataObject(InvInputHead obj) {
        this.daoSupport.updateDataTransaction(obj);
    }
    
    @Override
    public void insertOrUpdateDataObject(InvInputHead obj) {
        this.daoSupport.insertOrUpdateDataTransaction(obj);
    }

    @Override
    public void deleteDataObject(InvInputHead obj) {
        this.daoSupport.deleteDataTransactionJPA(obj);
    }

    @Override
    public List<InvInputHead> getDataObjects() {
        return this.daoSupport.getDataAllObject(InvInputHead.class);
    }

    @Override
    public InvInputHead getDataObject(int id) {
        return (InvInputHead)this.daoSupport.getDataObject(InvInputHead.class, id);
    }
    
    @Override
    public InvInputHead getDataObject(String code) {
        String sql = "select i.* from inv_input_head i where i.input_head_code = :code";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", code);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("i", InvInputHead.class);
        
        List<InvInputHead> list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list.size()>0) {
            return list.get(0);
        }
        
        return null;
    }
    
    @Override
    public List<InvInputHead> getDataObjects(InvInputHeadCO paramObj) {
        return null;
    }
    
    @Override
    public List<InvInputHead> getDataObjects(Pages pages) {
        return null;
    }
    
    @Override
    public List<InvInputHead> getDataObjects(Pages pages, InvInputHeadCO paramObj) {
        String sql = "select i.* from inv_input_head i where 1=1";
        
        Map<String, Object> args = new HashMap<String, Object>();
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "inputHeadCode", "and i.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "inputSourceType", "and i.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "inputSourceHeadCode", "and i.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "inputType", "and i.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "inputDate", "and i.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "status", "and i.", args);
        sql = sql + " order by i.input_head_id desc";
        
        this.daoSupport.validateDataObject(paramObj);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("i", InvInputHead.class);
        
        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }

    @Override
    public List<Map<String, Object>> getDataObjectsArray(Pages pages, InvInputHeadCO paramObj) {
        return null;
    }
    
    @Override
    @Permissions(PermissionType.DATA_AUTH)
    public List<InvInputHead> getDataObjectsForDataAuth(@SqlParam String dataAuthSQL, Pages pages, InvInputHeadCO paramObj) {
        String sql = "select i.* from inv_input_head i where 1=1 and is_del = 0";
        
        Map<String, Object> args = new HashMap<String, Object>();
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "inputHeadCode", "and i.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "inputSourceType", "and i.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "inputSourceHeadCode", "and i.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "inputType", "and i.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "inputDate", "and i.", args);
        sql = sql + DaoCRUDUtil.getSQLSearch(paramObj, "status", "and i.", args);
        sql = sql + DaoCRUDUtil.getDataAuthSQL(dataAuthSQL, "i.", "i.");
        sql = sql + " order by i.input_head_id desc";
        
        this.daoSupport.validateDataObject(paramObj);
        
        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("i", InvInputHead.class);
        
        return this.daoSupport.getDataSqlByPage(sql, entity, args, pages);
    }
    
    @Override
    public void updateApproveStatus(String code, String approveStatus) {
        String sql = "update inv_input_head set approve_status = :approveStatus";
        
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("code", code);
        args.put("approveStatus", approveStatus);
        
        if(approveStatus.equals("APPROVE")) {
            sql = sql + " ,status = 'CONFIRM'";
        }else if(approveStatus.equals("UNSUBMIT")) {
            sql = sql + " ,status = 'ALTER',version = version+1";
        }
        
        sql = sql + " where input_head_code = :code";
        
        this.daoSupport.executeSQLTransaction(sql, args);
    }


    @Override
    public InvInputHead getDataByCode(String inputHeadCode) {
        String sql = "select d.* from inv_input_head d where d.is_del = '0' and d.input_head_code = :inputHeadCode";

        Map<String, Object> args = new HashMap<String, Object>();
        args.put("inputHeadCode", inputHeadCode);

        Map<String, Class<?>> entity = new HashMap<String, Class<?>>();
        entity.put("d", InvInputHead.class);

        List<InvInputHead> list = this.daoSupport.selectDataSql(sql, entity, args);
        if(list.size()>0) {
            return list.get(0);
        }

        return null;
    }

    @Override
    public Integer getSumDataByType(String inputType) {
        String sql =
                "SELECT ih.* FROM inv_input_head ih WHERE ih.is_del = '0' AND ih.input_type = :inputType " +
                    "and DATE_FORMAT(ih.created_date, '%Y-%m-%d') = DATE_FORMAT(NOW(), '%Y-%m-%d')";
        Map<String, Object> args = new HashMap<>();
        args.put("inputType", inputType);
        Map<String, Class<?>> entity = new HashMap<>();
        entity.put("ih", InvInputHead.class);
        List<InvInputHead> result = daoSupport.selectDataSql(sql, entity, args);
        return result == null ? 0 : result.size();
    }

    @Override
    public SupplyMaterialVO getSupplyMaterialByCode(String purchase_order_id) {
        String sql = "SELECT a.*,b.vendor_name,c.send_time,c.total as price,c.status from supply_material a,md_vendor b,supply_material_r c "
                +"where a.vendor_id=b.vendor_id and a.id=c.supply_material_id and c.status=1 and a.purchase_order_id=:purchase_order_id";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("purchase_order_id", purchase_order_id);
        List<SupplyMaterialVO> list = this.daoSupport.selectDataSql(sql, SupplyMaterialVO.class, args);
        if(list.size()>0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<SupplyMaterialBeanVo> getSupplyMaterial(Pages pages, SupplyMaterialBeanVo supplyMaterialBeanVo){
        String sql = "SELECT a.*,b.vendor_name,c.send_time,c.total as price,c.status,d.contact_name,c.code,c.is_invoice from supply_material a,md_vendor b,supply_material_r c ,md_vendor_contact d "
                +"where a.vendor_id=b.vendor_id and a.id=c.supply_material_id and b.vendor_id = d.vendor_id and c.status=1";
        sql = sql + " order by a.create_time desc";
        List<SupplyMaterialBeanVo> result = this.daoSupport.getDataSqlByPage(sql,SupplyMaterialBeanVo.class,pages);
    return result;
    }

    @Override
    public List<SupplyMaterialDetailVO> getSupplyMaterialDetailByCode(String supply_material_id) {
        String sql = "SELECT d.*,'1' as material_name from supply_material_detail d where d.supply_material_id=:supply_material_id and d.status=1 order by d.id desc";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("supply_material_id", supply_material_id);
        List<SupplyMaterialDetailVO> list = this.daoSupport.selectDataSql(sql, SupplyMaterialDetailVO.class, args);
        return list;
    }

    @Override
    public SupplyMaterialDetailVO getMaterialDetailByCode(String id) {
        String sql = "SELECT a.*,'1' as material_name from supply_material_detail a where a.status=1 and a.id=:id";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("id", id);
        List<SupplyMaterialDetailVO> list = this.daoSupport.selectDataSql(sql, SupplyMaterialDetailVO.class, args);
        if(list.size()>0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public SupplyMaterialRVO getSupplyMaterialRByCode(String supply_material_id) {
        String sql = "SELECT a.* from supply_material_r a where a.supply_material_id=:supply_material_id";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("supply_material_id", supply_material_id);
        List<SupplyMaterialRVO> list = this.daoSupport.selectDataSql(sql, SupplyMaterialRVO.class, args);
        if(list.size()>0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public SupplyMaterialDetailVO getSupplyMaterialDetailById(String id) {
        String sql = "SELECT d.*,'1' as material_name from supply_material_detail d where d.id=:id and d.status=1 order by d.id desc";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("id", id);
        List<SupplyMaterialDetailVO> list = this.daoSupport.selectDataSql(sql, SupplyMaterialDetailVO.class, args);
        if(list.size()>0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public Integer getSupplyMaterialStatusCount(String supply_material_id) {
        String sql = "select count(1) as statusCount from supply_material_detail a where a.supply_material_id=:supply_material_id and a.status=1";
        Map<String, Object> args = new HashMap<String, Object>();
        args.put("supply_material_id", supply_material_id);

        List list = this.daoSupport.selectDataSqlCount(sql, args);
        if (list.size() > 0) {
            return this.daoSupport.convertSQLCount(list.get(0));
        }
        return 0;
    }

}
